﻿using System;
using System.Collections.Generic;
using Common;
using Common.Entities;
using Common.Models;
using OrderSales.Interface;



namespace OrderSales.Presenter
{
    class OrderFormPresenter
    {
        private Order _order;
        private IOrderForm _view;

        public OrderFormPresenter (Order order, IOrderForm view)
        {
            _view = view;
            _order = order;
            _view.DateOrder = order.DateOrder;
            _view.NumberOrder = order.NumberOrder;
            _view.Amount = _order.SumOrder;
            _view.DataSource = order.RecordOrders;

            _view.AddRecordOrder += AddOrderRecordFormShow;
            _view.RemoveRecordOrder += RemoveRecordOrder;
            
            _view.ChageDate += ChangeDate;
            _view.ChageNumber += Changenumber;
        }

        public void RefreshData ()
        {
            _order = DataManager.GetInstance().Orders.GetOrderById(_order.IdOrder);
            _view.DataSource = _order.RecordOrders;
            _view.Amount = _order.SumOrder;
        }

        public void AddOrderRecordFormShow(object sender, EventArgs e)
        {
            new SelectGoodsForm(_order).ShowDialog();
            RefreshData();
        }

        public void ChangeDate (object sender, EventArgs e)
        {
            _order.DateOrder = _view.DateOrder;
            DataManager.GetInstance().Orders.UpdateOrder(_order);
        }

        public void Changenumber(object sender, EventArgs e)
        {
            _order.NumberOrder = _view.NumberOrder;
            DataManager.GetInstance().Orders.UpdateOrder(_order);
        }

        public void RemoveRecordOrder(object sender, EventArgs e)
        {
            DataManager.GetInstance().OrderRecords.DeleteOrderRecord(((List<OrderRecord>)_view.DataSource)[(int)_view.Current].IdOrderRecord);
            RefreshData();
        }
    }
}
